export function formatTimestamp(timestamp) {
  const date = new Date(timestamp);
  const now = new Date();

  if (isSameDay(date, now)) {
    return formatDate(date, "hh:mm:ss");
  } else if (isSameYear(date, now)) {
    return formatDate(date, "MM-dd");
  } else {
    return formatDate(date, "yyyy-MM-dd");
  }
}

function isSameDay(date1, date2) {
  return (
    date1.getFullYear() === date2.getFullYear() &&
    date1.getMonth() === date2.getMonth() &&
    date1.getDate() === date2.getDate()
  );
}

function isSameYear(date1, date2) {
  return date1.getFullYear() === date2.getFullYear();
}

function formatDate(date, format) {
  const year = date.getFullYear();
  const month = pad(date.getMonth() + 1);
  const day = pad(date.getDate());
  const hours = pad(date.getHours());
  const minutes = pad(date.getMinutes());
  const seconds = pad(date.getSeconds());

  return format
    .replace("yyyy", year)
    .replace("MM", month)
    .replace("dd", day)
    .replace("hh", hours)
    .replace("mm", minutes)
    .replace("ss", seconds);
}

function pad(value) {
  return String(value).padStart(2, "0");
}

export function formatDateStrToChinese(dateString) {
  const date = new Date(dateString);
  const year = date.getFullYear();
  const month = ("0" + (date.getMonth() + 1)).slice(-2);
  const day = ("0" + date.getDate()).slice(-2);
  const formattedDate = year + "年" + month + "月" + day + "日";
  return formattedDate;
}

export function formatDateStrToDate(dateString) {
  const now = new Date();
  const inputDate = new Date(dateString);

  // 如果是今天的日期，则只返回时分
  if (now.toDateString() === inputDate.toDateString()) {
    const hours = inputDate.getHours().toString().padStart(2, "0");
    const minutes = inputDate.getMinutes().toString().padStart(2, "0");
    return hours + ":" + minutes;
  }

  // 如果是今年的日期，则只返回月日时分
  if (now.getFullYear() === inputDate.getFullYear()) {
    const month = (inputDate.getMonth() + 1)
      .toString()
      .padStart(2, "0")
      .replace(/^0+/, "");
    const day = inputDate
      .getDate()
      .toString()
      .padStart(2, "0")
      .replace(/^0+/, "");
    const hours = inputDate.getHours().toString().padStart(2, "0");
    const minutes = inputDate.getMinutes().toString().padStart(2, "0");
    return month + "-" + day + " " + hours + ":" + minutes;
  }

  // 如果不是今年的日期，则只返回年月日
  const year = inputDate.getFullYear().toString();
  const month = (inputDate.getMonth() + 1)
    .toString()
    .padStart(2, "0")
    .replace(/^0+/, "");
  const day = inputDate
    .getDate()
    .toString()
    .padStart(2, "0")
    .replace(/^0+/, "");
  return year + "-" + month + "-" + day;
}

export function dateStringToTimestamp(dateString) {
  var date = new Date(dateString);
  var timestamp = date.getTime();
  return timestamp;
}

// export function formatTime(seconds) {
//   if (seconds < 60) {
//     return `${seconds}s`;
//   } else if (seconds < 3600) {
//     const minutes = Math.floor(seconds / 60);
//     const remainingSeconds = seconds % 60;
//     return `${minutes}:${remainingSeconds}`;
//   } else {
//     const hours = Math.floor(seconds / 3600);
//     const remainingMinutes = Math.floor((seconds % 3600) / 60);
//     const remainingSeconds = seconds % 60;
//     return `${hours}:${remainingMinutes}:${remainingSeconds}`;
//   }
// }
